package XuLieHua;

import java.io.*;

/**
 * User: cool
 * Date: 2019/12/27
 * Time: 15:59
 */
public class TransientTest {
    public static void main(String[] args) throws Exception {
        User1 user1 = new User1("我是一个人", 18);
        System.out.println("序列化之前");
        System.out.println("name: " + user1.getName());
        System.out.println("age: " + user1.getAge());
        ObjectOutputStream os1 = new ObjectOutputStream(new FileOutputStream("d:/user.txt"));
        os1.writeObject(user1);
        os1.flush();
        os1.close();
        ObjectInputStream is1 = new ObjectInputStream(new FileInputStream("d:/user.txt"));
        user1 = (User1) is1.readObject();
        is1.close();
        System.out.println("序列化之后");
        System.out.println("name: " + user1.getName());
        System.out.println("age: " + user1.getAge());
    }
}
class User1 implements Externalizable {
    private static final long serialVersionUID = -4613115939592042963L;
    private transient String name;
    private Integer age;
    public User1(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
    public User1() {
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeObject(name);
        out.writeInt(age);
    }
    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        name = (String) in.readObject();
        age = in.readInt();
    }
}
